<?php
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Http\Controllers\Api\v1\Astro\DataController;
use Illuminate\Http\Request;

use App\Models\Api\Recode;


class CompositeprogressionsController extends BaseController
{
    /**
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get(Request $request)
    {
        $Data = new DataController();
        $Set = new AstrosetController();
        $astro_set = $Set->get(true, 'composite_progressions');// 内部调用
        $h_sys = $astro_set['value']['h_sys'];
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $map = ['id' => $request->input('id1', 0), 'uid' => $request->uid];

        $RecodeModel = new Recode();

        $userinfo1 = $RecodeModel->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo1);
        $longitude1 = ($userinfo1['long_deg'] + $userinfo1['long_min'] / 60) * $userinfo1['ew'];
        $latitude1 = ($userinfo1['lat_deg'] + $userinfo1['lat_min'] / 60) * $userinfo1['ns'];

        $map['id'] = $request->input('id2', 0);

        $userinfo2 = $RecodeModel->getRecode($map);
        $timestamp2 = $Data->deal_time($userinfo2);
        $longitude2 = ($userinfo2['long_deg'] + $userinfo2['long_min'] / 60) * $userinfo2['ew'];
        $latitude2 = ($userinfo2['lat_deg'] + $userinfo2['lat_min'] / 60) * $userinfo2['ns'];

        $datetime = htmlspecialchars($request->input('datetime'));
        $time = strtotime($datetime);
        $timeinfo = [
            'year' => date('Y', $time), 'month' => date('m', $time), 'day' => date('d', $time),
            'hour' => date('H', $time), 'minute' => date('i', $time),
        ];
        $timeinfo['timezone'] = intval($request->input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'], $timeinfo['minute'], 0,
            $timeinfo['month'], $timeinfo['day'], $timeinfo['year']) ? 1 : 0;

        $timestamp3 = $Data->deal_time($timeinfo);

        $birth_JD1 = gregoriantojd(date('m', $timestamp1), date('d', $timestamp1), date('Y', $timestamp1)) + ((date('H', $timestamp1) + (date('i', $timestamp1) / 60)) / 24);
        $birth_JD2 = gregoriantojd(date('m', $timestamp2), date('d', $timestamp2), date('Y', $timestamp2)) + ((date('H', $timestamp2) + (date('i', $timestamp2) / 60)) / 24);
        $start_JD = gregoriantojd(date('m', $timestamp3), date('d', $timestamp3), date('Y', $timestamp3)) + ((date('H', $timestamp3) + (date('i', $timestamp3) / 60)) / 24);
        $days_alive1 = $start_JD - $birth_JD1; // 总天数
        $days_alive2 = $start_JD - $birth_JD2; // 总天数
        $prog_time_to_add1 = $days_alive1 / 365.2422;// 365.2422  365.2421990  365.25
        $prog_time_to_add2 = $days_alive2 / 365.2422;// 365.2422  365.2421990  365.25

        $time_before_JD1 = $birth_JD1 + floor($prog_time_to_add1);
        $time_before_JD2 = $birth_JD2 + floor($prog_time_to_add2);

        //变化比率 //就是$prog_time_to_add的去掉整数的小数
        $time_change_pro1 = $prog_time_to_add1 - floor($prog_time_to_add1);
        $time_change_pro2 = $prog_time_to_add2 - floor($prog_time_to_add2);

        $date_before1 = strtotime(jdtogregorian($time_before_JD1));
        $date_after1 = strtotime('+1 day', $date_before1);

        $date_before2 = strtotime(jdtogregorian($time_before_JD2));
        $date_after2 = strtotime('+1 day', $date_before2);

        $utdatenow1 = strftime("%d.%m.%Y", $date_before1);
        $utnow1 = strftime("%H:%M:%S", $timestamp1);

        $utdatenow2 = strftime("%d.%m.%Y", $date_after1);
        $utnow2 = strftime("%H:%M:%S", $timestamp1);

        $utdatenow3 = strftime("%d.%m.%Y", $date_after2);
        $utnow3 = strftime("%H:%M:%S", $timestamp2);

        $utdatenow4 = strftime("%d.%m.%Y", $date_after2);
        $utnow4 = strftime("%H:%M:%S", $timestamp2);

        $options1 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $h_sys];
        $options2 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $h_sys];
        $options3 = ['utdatenow' => $utdatenow3, 'utnow' => $utnow3, 'longitude' => $longitude2, 'latitude' => $latitude2, 'h_sys' => $h_sys];
        $options4 = ['utdatenow' => $utdatenow4, 'utnow' => $utnow4, 'longitude' => $longitude2, 'latitude' => $latitude2, 'h_sys' => $h_sys];

        $out1 = $Data->execData($options1);
        $out2 = $Data->execData($options2);
        $out3 = $Data->execData($options3);
        $out4 = $Data->execData($options4);

        $calOut1 = $Data->getProgressionThirdprogressed($out1, $out2, $time_change_pro1);
        $calOut2 = $Data->getProgressionThirdprogressed($out3, $out4, $time_change_pro2);


        $original_data1 = $Data->getOriginalData($calOut1, $h_sys);
        $original_data2 = $Data->getOriginalData($calOut2, $h_sys);

        $original_data = $Data->getTheCompoisteCenter($original_data1, $original_data2);

        $Data->array_remove($original_data, $Data->planets_display);
        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1']);
        $phase = $Data->planetPkPlanet($original_data, $original_data, true);
        foreach ($phase['planets'] as $k => $d) {
            $details['planets'][$k]['phase'] = $d['phase'];
            if (empty($d['phase'])) $details['planets'][$k]['phase'] = null;
        }
        $details['phase'] = $phase['phase'];

        $details['planets'] = (object)$details['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        return response()->json(['code' => 200, 'data' => $details, 'msg' => 'Success.']);
    }
}
